package com.tsinghuabigdata.edu.ddmath.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.tsinghuabigdata.edu.ddmath.R;


/**
 * 在线辅导申请Dialog
 */
public class TutorDialog extends Dialog implements View.OnClickListener {


    private TextView  mTvTutorTime;
    private Button    mBtnStart;
    private ImageView mDialogQreviewTipimage;
    private int second = 60;
    Handler handler = new Handler();

    private TutorDialogListener mTutorDialogListener;

    public TutorDialog(Context context) {
        super(context);
        initData();
    }

    public TutorDialog(Context context, int theme) {
        super(context, theme);
        initData();
    }


    /**
     * 普通提示框
     *
     * @param message   文本信息
     * @param poBtnName 确认回调
     */
    public void setData(String message, String poBtnName) {
        mTvTutorTime.setText(message);
        mBtnStart.setText(poBtnName);
    }

    public void startCount() {
        handler.postDelayed(runnable, 1000);
    }


    private void initData() {
        setContentView(R.layout.dialog_tutor);
        mTvTutorTime = (TextView) findViewById(R.id.tv_tutor_time);
        mBtnStart = (Button) findViewById(R.id.btn_start);
        mDialogQreviewTipimage = (ImageView) findViewById(R.id.dialog_qreview_tipimage);
        mBtnStart.setOnClickListener(this);

        setCancelable(false);
        setCanceledOnTouchOutside(false);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_start:
                mTutorDialogListener.select(true);
                handler.removeCallbacks(runnable);
                break;
        }
    }


    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            second--;
            mTvTutorTime.setText("老师已经同意您的在线辅导申请，请在" + second + "s内开始。");
            if (second > 0) {
                handler.postDelayed(this, 1000);
            } else {
                mTutorDialogListener.select(false);
            }
        }
    };

    public interface TutorDialogListener {
        void select(boolean click);
    }

    public void setTutorDialogListener(TutorDialogListener tutorDialogListener) {
        mTutorDialogListener = tutorDialogListener;
    }

    public void setSecond(int second) {
        this.second = second;
    }
}
